package com.uyayo.ucenter.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.framework.common.exception.EmailMultipleException;
import com.uyayo.framework.common.exception.PhoneMultipleException;
import com.uyayo.framework.core.service.impl.BaseServiceImpl;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import com.uyayo.ucenter.entity.Group;
import com.uyayo.ucenter.entity.GroupResourceRelation;
import com.uyayo.ucenter.mapper.GroupMapper;
import com.uyayo.ucenter.mapper.GroupResourceRelationMapper;
import com.uyayo.ucenter.model.GroupCondition;
import com.uyayo.ucenter.service.GroupService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.List;


@Service("groupService")
public class GroupServiceImpl extends BaseServiceImpl<Group> implements GroupService {

	private final GroupMapper groupMapper;

	private final GroupResourceRelationMapper groupResourceRelationMapper;
	public GroupServiceImpl(GroupMapper groupMapper, GroupResourceRelationMapper groupResourceRelationMapper) {
		this.groupMapper = groupMapper;
		this.groupResourceRelationMapper = groupResourceRelationMapper;
	}

	@Override
	public List<Group> selectGroup(GroupCondition groupCondition) {
		Example example = new Example(Group.class);
		example.createCriteria();
		return groupMapper.selectByExample(example);
	}
	
	@Override
	public PageBean<Group> selectGroupByPaging(PageBean<Group> pageBean,GroupCondition groupCondition) {

		List<Group> userList;
		//分页处理
		List<OrderBy> orderByList = new ArrayList<>();
		orderByList.add(new OrderBy("createTime", "create_time"));
		orderByList.add(new OrderBy("updateTime", "update_time"));

		OrderByUtil  orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderByList, pageBean.getOrderField(), pageBean.getOrderDirection());
		PageHelper.startPage(pageBean.getPageCurrent(), pageBean.getPageSize());
		
		Example example = new Example(Group.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(groupCondition.getName())){
			criteria.andLike("name","%"+groupCondition.getName()+"%");
		}
		if(groupCondition.getBeginCreateTime()!=null || groupCondition.getEndCreateTime()!=null){
			criteria.andBetween("createTime", groupCondition.getBeginCreateTime(), groupCondition.getEndCreateTime());
		}
		userList = groupMapper.selectByExample(example);

		//取记录总条数
		PageInfo<Group> pageInfo = new PageInfo<>(userList);
		pageBean.setTotalCount(pageInfo.getTotal());
		pageBean.setList(userList);
		return pageBean;
	}
	

	@Override
	public int saveGroup(Group group) throws EmailMultipleException, PhoneMultipleException {
		return groupMapper.insertSelective(group);
	}

	@Override
	public int updateGroup(Group group) throws EmailMultipleException, PhoneMultipleException {
		return groupMapper.updateByPrimaryKeySelective(group);
	}

	@Override
	public List<GroupResourceRelation> findGroupResourceRelation(Long groupId) {
		Example example = new Example(GroupResourceRelation.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("groupId", groupId);
		return groupResourceRelationMapper.selectByExample(example);
	}


	@Override
	public int grantGroupResource(String ids,Long groupId) {
		int saveCount = 0;
		String[] resourceIdArr = ids.split(",");
		List<GroupResourceRelation> groupResourceRelations = new ArrayList<>();
		if(resourceIdArr.length>0 && !resourceIdArr[0].equals("")){
			for (String aResourceIdArr : resourceIdArr) {
				GroupResourceRelation groupResourceRelation = new GroupResourceRelation();
				groupResourceRelation.setGroupId(groupId);
				groupResourceRelation.setResourceId(Long.valueOf(aResourceIdArr));
				groupResourceRelations.add(groupResourceRelation);
			}
		}
		Example example = new Example(GroupResourceRelation.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("groupId", groupId);
		if(groupResourceRelations.size()>0){
			saveCount = groupResourceRelationMapper.insertGroupResourceRelationByList(groupResourceRelations);
		}
		return saveCount;
	}
}
